<?php
    /**
     * Created by PhpStorm.
     * User: wzh
     * Date: 2016/6/15 0015
     * Time: 10:59
     */
    namespace wzh\tcfxapi;
    class Order {
        public $jsondata;

        //创建订单前校验
        public function CreateOrderValidate() {
            $tongcheng = new Tongcheng();
            $jsondata = $this->jsondata;
            $data = array();
            $data['PriceId'] = $jsondata['PriceId'];//*同程对接产品id
            $data['TravelDate'] = $jsondata['TravelDate'];//*出游日期
            $data['Tickets'] = $jsondata['Tickets'];//*取票数
            isset($jsondata['TCAmount']) && $data['TCAmount'] = $jsondata['TCAmount'];//同程价如果大于0，则表示需要校验
            isset($jsondata['AgentAmount']) && $data['AgentAmount'] = $jsondata['AgentAmount'];//分销结算总价,大于零则表示校验
            isset($jsondata['TravelerIdCardNo']) && $data['TravelerIdCardNo'] = $jsondata['TravelerIdCardNo'];//取票身份证
            $data['TravelerMobile'] = $jsondata['TravelerMobile'];//*取票人手机号
            isset($jsondata['MobileIdentifier']) && $data['MobileIdentifier'] = $jsondata['s_id'];//预定人手机号码识别码
            //实名制列表，姓名，手机，身份证，邮箱
            isset($jsondata['RealBookInfo']) && $data['RealBookInfo'] =$jsondata['RealBookInfo']; //游客身份信息
            $data['BookEmail'] = $jsondata['BookEmail'];//预定人邮箱
            //场次信息，如果场次id，场次开始时间，场次结束时间
            isset($jsondata['ScreeningInfo']) && $data['ScreeningInfo'] = $jsondata['ScreeningInfo'];
            /*   isset($jsondata['ScreeningInfo']) && $data['ScreeningInfo'] = array(
                "ScreeningId" => $jsondata['s_id'],
                "BeginTime"   => $jsondata['s_id'],
                "EndTime"     => $jsondata['s_id'],
            );*/
            $response = $tongcheng->request($data, 'CreateOrderValidate');
            $response = json_decode($response, true);
            if (empty($response) || $response['respCode'] != 1) {
                Tongcheng::writelog($response, 'error', 'CreateOrderValidate.log');//失败写入日志。
                $response = false;
            }
            return $response;
        }

        //创建订单
        /*
        正确时会返回：
        "orderResponse": [
        {
            "serialId": "sz53a90d7u1012852191"
        }
    ]
6002，已存在订单时返回：
    "orderResponse": [
        {
            "thirdSerialId": "0000111111111",
            "tcSerialId": "sz53b3a38b1012852474"
        }
    ]
         * */
        public function CreateOrder() {
            $tongcheng = new Tongcheng();
            $jsondata = $this->jsondata;
            $data = array();
            /*$data['ticketsNum'] = $jsondata['ticketsNum'];//*取票人购买数量list
            $data['bookMan'] = $jsondata['bookMan'];//*取票人list
            $data['bookMobile'] = $jsondata['bookMobile'];//*取票人手机号码list
            */
            $data['bookMobileType'] = 1;//*预定人手机类型1：大陆 0：非大陆
            /*
            $data['bookAddress'] = $jsondata['address'];//*取票地址
            $data['bookPostCode'] = $jsondata['postcode'];//*取票邮编
            $data['bookEmail'] = $jsondata['email'];//*取票人邮箱
            $data['travelerName'] = array($jsondata['name']);//*游玩人list
            $data['travelerMobile'] = array($jsondata['mobile']);//*游玩人人手机号码list
            */
            $data['travelerMobileType'] = 1;//*游玩人手机号码类型
            //$data['priceID'] = $jsondata['priceID'];//*同程对接产品id
            //$data['travelDate'] = $jsondata['travelDate'];//*出游日期
            isset($jsondata['remark'])&&$data['remark'] = $jsondata['remark'];//实名制内容
            /*
            $data['travelerIdCardNo'] = $jsondata['cardno'];//预定人身份证号，如果需要则必填
            $data['travelerIdCardNoList'] = array($jsondata['mobile']);//游玩人身份证list当产品验证方式为实名制验证+身份证验证双重验证方式时，有几个取票人就要求传递几个取票身份证
            $data['thirdSerialId'] = array($jsondata['order_id']);//联合订单号list，主要用来验证是否重复提交
            */
            //场次信息，如果场次id，场次开始时间，场次结束时间 时间格式：yyyy-MM-dd HH:mm:ss
            if (isset($jsondata['sceneryScreeningInfo'])) {
                $data['sceneryScreeningInfo'] = $data['sceneryScreeningInfo'];
            }
            $data=array_merge($jsondata,$data);
            $response = $tongcheng->request($data, 'CreateOrder');
            $response = json_decode($response, true);
            //6002为重复下单。需处理
            if (!empty($response) && (($response['respCode'] == 1) || ($response['respCode'] != 6002))) {
                return $response;
            }
            else {
                Tongcheng::writelog($response, 'error', 'CreateOrder.log');//失败写入日志。
                return false;
            }
        }

        //重发凭证
        public function SendMessage() {
            $tongcheng = new Tongcheng();
            $jsondata = $this->jsondata;
            $data = array();
            $data['serialId'] = array($jsondata['s_order']);//*同程流水号
            $data['sendMobile'] = $jsondata['mobile'];//*取票人手机号
            $response = $tongcheng->request($data, 'SendMessage');
            $response = json_decode($response, true);
            if (empty($response) || ($response['respCode'] != 1)) {
                Tongcheng::writelog($response, 'error', 'SendMessage.log');//失败写入日志。
            }
            return $response;
        }

        /*
         * 根据同程订单id，进行订单查询
         * 订单状态(N:客户提交；I：已发传真；F：已确认；J：景区已确认；V：已核单；C：取消；S：NoShow),如果不传该参数，默认查看所有状态的订单
         * @return array|bool|$orderlist
         * eg json:{"respCode": 1,"respMsg": "获取数据成功","respTime": "2014-5-24 14:36:08","totalCount": 121,
         "orderList": [
        {
            "orderSerialId": "sz5254bcu52100217852",
            "travelDate": "2013-05-10",
            "createTime": "2013-10-9 10:18:10",
            "orderState": "F",
            "ticketsNumber": 1
        },
        {
            "orderSerialId": "sz5254u8da2100217388",
            "travelDate": "2013-05-05",
            "createTime": "2013-10-9 13:25:42",
            "orderState": "F",
            "ticketsNumber": 2
        }
    ]
}*/
        public function QueryOrderList() {
            $tongcheng = new Tongcheng();
            $jsondata = $this->jsondata;
            $data = array();
            isset($jsondata['s_order'])&&($data['orderSerialId'] = $jsondata['s_order']);//同程订单号
            isset($jsondata['orderState'])&&($data['orderState'] = $jsondata['state']);//同程订单状态
            isset($jsondata['travelDateFrom'])&&($data['travelDateFrom'] = $jsondata['travelDateFrom']);//出游开始时间 2014-05-01
            isset($jsondata['travelDateTo'])&&($data['travelDateTo'] = $jsondata['travelDateTo']);//出游结束时间 2014-05-01
            $data['pageIndex'] = $jsondata['pageIndex'] ?: 1;//*页码索引
            $data['pageSize'] = $jsondata['pageSize'] ?: 10;//*页码数量
            $response = $tongcheng->request($data, 'QueryOrderList');
            $response = json_decode($response, true);
            if (empty($response) || ($response['respCode'] != 1)) {
                Tongcheng::writelog($response, 'error', 'QueryOrderList.log');//失败写入日志。
                $response = false;
            }
            return $response;
        }

        /**
         * 根据联合订单id，进行订单查询
         * 订单状态(N:客户提交；I：已发传真；F：已确认；J：景区已确认；V：已核单；C：取消；S：NoShow),如果不传该参数，默认查看所有状态的订单
         * @return bool|mixed|string
         * eg json:{"respCode": 1,"respMsg": "获取数据成功","respTime": "2014-8-1 18:20:39",
         * "orderList": [
         * {
         * "thirdSerialId": "sz0001",
         * "tcSerialId": "sz53b2ad1b1012852557",
         * "createTime": "2014-7-1 20:44:09",
         * "travelDate": "2014-07-10",
         * "orderState": "C",
         * "payState": "N"
         * }
         * ]
         * }
         */
        public function GetOrderListByThirdSerialId() {
            $tongcheng = new Tongcheng();
            $jsondata = $this->jsondata;
            $data = array();
            $data['orderSerialId'] = array($jsondata['order_id']);//*系统订单号
            $response = $tongcheng->request($data, 'GetOrderListByThirdSerialId');
            $response = json_decode($response, true);
            if (empty($response) || ($response['respCode'] != 1)) {
                Tongcheng::writelog($response, 'error', 'CreateOrder.log');//失败写入日志。
                $response = false;
            }
            return $response;
        }

        /**
         * 取消订单申请允许部分退和全部退 目前退款类型只能为全部退款
         * @return bool|mixed|string
         * eg:{
         * "respCode": 1,
         * "respMsg": "申请退款成功",
         * "respTime": "2014-10-31 12:37:10.285"
         * }
         */
        public function NewCreateOrderCancel($jsondata=false) {
            $tongcheng = new Tongcheng();
            $jsondata =$jsondata?: $this->jsondata;
            $data = array();
            $data['serialId'] = $jsondata['s_order'];//*同程订单号
            isset($jsondata['re_quantity']) && ($data['refundTicketsNum'] = $jsondata['re_quantity']);//退款数量，全部退款可以不传退票数量
            //$data['RefundType'] = iseet($jsondata['refund_type'])?$jsondata['refund_type']:1;//*退款类型 0：部分退款 1：全部退款
            $data['RefundType'] = 1;//*退款类型 0：部分退款 1：全部退款
            $data['RefundReason'] = $jsondata['RefundReason']?:'申请退款';//退款原因
            $response = $tongcheng->request($data, 'NewCreateOrderCancel');
            $response = json_decode($response, true);
            if (empty($response) || ($response['respCode'] != 1)) {
                Tongcheng::writelog($response, 'error', 'eOrderCancel.log');//失败写入日志。
            }
            return $response;
        }

        /**
         *定时查询核销接口
         * @return mixed|string
         */
        public function GetOrderVerification() {
            $tongcheng = new Tongcheng();
            $jsondata = $this->jsondata;
            $data = array();
            $data['maxIncrementId'] = $jsondata['maxIncrementId'];//增量查询接口
            $data['queryNumber'] = $jsondata['queryNumber'];//查询数量
            $response = $tongcheng->request($data, 'GetOrderVerification');
            $response = json_decode($response, true);
            if (empty($response) || ($response['respCode'] != 1)) {
                Tongcheng::writelog($response, 'error', 'GetOrderVerification.log');//失败写入日志。
            }
            return $response;
        }

        /**
         * 订单反馈接口返回
         * @return bool
         * eg："requestBody": {
         * "OrderSerialId": "sz55500b801012852712",
         * "FeedBackType": 3,
         * "MobileNumber": "15851420220",
         * "FeedBackTitle": "核实订单是否可以退款",
         * "FeedBackContent": "核实订单是否可以退款",
         * "FeedBackRemark": "核实订单是否可以退款，请确认"
         * }
         */
        public function AddOrderFeedBack() {
            $tongcheng = new Tongcheng();
            $jsondata = $this->jsondata;
            $data = array();
            $data['OrderSerialId'] = $jsondata['s_order'];//*订单流水号
            $data['FeedBackType'] = $jsondata['error_type'];//反馈类型
            $data['MobileNumber'] = $jsondata['mobile'];//*游客手机号
            $data['FeedBackTitle'] = $jsondata['feed_back_title'];//*反馈标题
            $data['FeedBackContent'] = $jsondata['feed_back_content'];//*反馈内容
            $data['FeedBackRemark'] = $jsondata['feed_back_remark'];//反馈备注
            $response = $tongcheng->request($data, 'AddOrderFeedBack');
            $response = json_decode($response, true);
            if (empty($response) || ($response['respCode'] != 1)) {
                Tongcheng::writelog($response, 'error', 'AddOrderFeedBack.log');//失败写入日志。
                $response = false;
            }
            return $response;
        }

        /**
         *主要是校验退款接口是否验证签名通过。
         * @return array
         * request $jsondata:{
         * "requestHead": {
         * "digitalSign": "2201503d5e539be5ee249a85abfcfdff",
         * "agentAccount": "bc083ca5-4f37-40f0-9fc6-638f5d920981"
         * },
         * "requestBody": {
         * "returnCode": "1",
         * "returnMsg": "退款成功",
         * "refundTime": "2015-03-31 18:14:18",
         * "serialId": "sz551a6b262103334298",
         * "RefundType": 1,
         * "refundTicketsNum": 1,
         * "refundAmount": 100,
         * "poundageAmount": 2.5
         * }
         * }
         */
        public function checkCanelSign($jsondata) {
            $tongcheng = new Tongcheng();
            $data = $jsondata['requestBody'];
            //$sign=$jsondata['requestHead']['digitalSign'];
            $sign = $tongcheng->OrderCancelSign($data);
            if (($sign != $jsondata['requestHead']['digitalSign']) || (\wzh\tcfxapi\Tongcheng::$agentAccount != $jsondata['requestHead']['agentAccount'])) {
                return false;
            }
            return true;
        }

    }
